package com.exam.system.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zj
 * createTime: 2017/11/27
 */
public class DynamicFields {

    public static Logger log = LoggerFactory.getLogger(DynamicFields.class);

    private Object[][] fields;

    public DynamicFields (int initialSize){
        fields = new Object[initialSize][2];
        for(int i=0;i< initialSize;i++){
            fields[i] = new Object[]{null,null};
        }
    }

    public String toString(){
        StringBuilder result = new StringBuilder();
        for (Object[] obj:fields) {
            result.append(obj[0]);
            result.append(":");
            result.append(obj[1]);
            result.append("\n");
        }
        return result.toString();
    }

    private int hasField(String id){
        for(int i=0;i<fields.length;i++){
            if(id.equals(fields[i][0])){
                return i;
            }
        }
        return -1;
    }

    private int getFieldNumber(String id)throws  NoSuchFieldException{
        int fieldNum = hasField(id);
        if(fieldNum == -1){
            throw new NoSuchFieldException();
        }
        return fieldNum;
    }

    private int makeField(String id){
        for(int i=0;i<fields.length;i++){
            if(fields[i][0] == null){
                fields[i][0] = id;
                return i;
            }
        }

        Object[][] tmp = new Object[fields.length+1][2];
        for(int i=0;i<fields.length;i++){
            tmp[i] = fields[i];
        }
        fields = tmp;
        return makeField(id);
    }

    public Object getField(String id)throws NoSuchFieldException{
        return fields[getFieldNumber(id)][1];
    }

    public Object setField(String id,Object value)throws DynamicFieldException{
        if(value == null){
            DynamicFieldException dfe = new DynamicFieldException();
            dfe.initCause(new NullPointerException());
            throw dfe;
        }

        int fieldNumber = hasField(id);
        if(fieldNumber == -1){
            fieldNumber = makeField(id);
        }
        Object result;

        try {
            result = getField(id);
        } catch (NoSuchFieldException e) {
            throw  new RuntimeException();
        }
        fields[fieldNumber][1] = value;
        return result;
    }

    public static void main(String[] args) {
        DynamicFields df = new DynamicFields(3);
        System.out.println(df);

        try {
            df.setField("d","A value for d");
            df.setField("number",47);
            df.setField("number2",48);
            System.out.println(df);
            df.setField("d","A new value for d");
            df.setField("number3",11);
            System.out.println("df:"+df);
            System.out.println("df.getField(\"d\"):"+df.getField("d"));
            //存放null值抛出异常
            Object field = df.setField("d",null);
        } catch (DynamicFieldException e) {
            e.printStackTrace();
//            System.out.println("DynamicFieldException: " + e.getMessage());
            log.debug("DynamicFieldException: " + e.getMessage());
        } catch (NoSuchFieldException e) {
            System.out.println("NoSuchFieldException: " + e.getMessage());
        }


    }
}
